package org.jf.tool.fx.jftoolfx.common;

import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.jf.tool.fx.jftoolfx.Main;
import org.jf.tool.fx.jftoolfx.util.JavaFXUtil;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * @author jifeng_zhang
 * @date 2025/8/14 下午7:08
 */
public abstract class BaseController extends Main implements Initializable {

    /**
     * 文件保存上次目录
     */
    protected static File LAST_DIRECTORY = null;

    /**
     * 菜单栏ID
     */
    private static final Map<String, String> MENU_BAR_IDS = new HashMap<>();

    static {
        MENU_BAR_IDS.put("xmlFormat", "format/xml.fxml");
        MENU_BAR_IDS.put("jsonFormat", "format/json.fxml");
        MENU_BAR_IDS.put("hash", "encrypt/hash.fxml");
        MENU_BAR_IDS.put("hmac", "encrypt/hmac.fxml");
        MENU_BAR_IDS.put("base64", "encrypt/base64.fxml");
        MENU_BAR_IDS.put("symmetric", "encrypt/symmetric.fxml");
        MENU_BAR_IDS.put("asymmetric", "encrypt/asymmetric.fxml");
        MENU_BAR_IDS.put("settings", "settings/settings.fxml");
        MENU_BAR_IDS.put("cron", "tools/cron/cron.fxml");
        MENU_BAR_IDS.put("ascii", "document/ascii.fxml");
        MENU_BAR_IDS.put("httpCode", "document/httpCode.fxml");
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        init(url, resourceBundle);
    }

    protected abstract void init(URL url, ResourceBundle resourceBundle);

    public static <T> T getFxml(String menuBarId) {
        String fxml = MENU_BAR_IDS.get(menuBarId);
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource(fxml));
        try {
            return fxmlLoader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void copy(String text) {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent content = new ClipboardContent();
        content.putString(text);
        clipboard.setContent(content);
        JavaFXUtil.msg(TOP_STAGE, "已复制到剪切板！");
    }

}
